function debounce (fn, delay) {
    let timer = null
    return (...args) => {
        clearTimeout(timer)
        timer = setTimeout(() => {
            fn.apply(this, args)
        }, delay)
    }
}

// Test
function f1 (a) {
    console.log(123, '+', a)
}

const debounceFn = debounce(f1, 300)
debounceFn(1)
debounceFn(2)
debounceFn(3)
debounce(f1, 300)(4)
debounce(f1, 300)(5)
